package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import Database.DatabaseConnector;

@WebServlet("/AddReviewerServlet")
public class AddReviewerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DatabaseConnector db = new DatabaseConnector();
		HttpSession session = request.getSession();
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		
		System.out.println(userName);
		System.out.println(password);
		System.out.println(email);
		System.out.println("here");
		
		db.addReviewer(userName,password, email);
		
		if( db.authenticateReviewer(userName, password))
		{
			 session.setAttribute("subloggedIn", true);
			 session.setAttribute("subUser", userName);
			 session.setAttribute("subPass", password);
			 session.setAttribute("subEmail", email);

			 JSONObject subInfo = new JSONObject();

			 try {
				subInfo.put("SubUserName", userName);
				subInfo.put("SubPass", password);
				subInfo.put("SubEmail", email);
				
				
				response.getWriter().print(subInfo.toString());
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
			}
		}
		else
		{
			System.out.println("You Fail!");
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}

}
